package com.kgc.scd.util;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * @program: com.kgc.scd.util
 * @author: HarryXzy
 * @create: 2022-06-10 10:45
 * @description: 常用工具方法
 **/
public class MyUtils {
    /**
     * @author: HarryXzy
     * @date: 2022/5/31 15:28
     * @return: java.lang.String
     * @description: 生成32位以内，指定长度随机字符串，使用UUID
     */
    public static String  generateNonceStrUseUUID(int length){
        // 长度必须在1-32之间
        return (length > 32 || length <1) ? UUID.randomUUID().toString().replace("-","").substring(0,32)
                : UUID.randomUUID().toString().replace("-","").substring(0,length);
    }

    /**
     * @author: HarryXzy
     * @date: 2022/5/31 11:04
     * @return: null
     * @description: 生成MD5签名
     **/
    public static String MD5(String data) throws Exception {
        java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }
}
